package com.android.sharecar.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.android.sharecar.model.User;

/**
 * class:SignupStep1Activity
 *  Deals with the signup transactions, 
 *  include personal informations and head picture
 * date:	2011-9-7
 * @author xinyan
 * 
 */
public class SignupStep1Activity extends Activity {
	public static final String TAG = "SignupStep1Activity";

	// member vars
	private EditText mEmail; // var for email edit text
	private EditText mPassword; // var for password edit text
	private EditText mConfirmPwd; // var for confirm password
	private EditText mNickName; // var for nickname
	private Button mNext; // var for next button

	// constants
	public static final String ALERT_TYPE_ILLEGAL_EMAIL = "illegal email";
	public static final String ALERT_TYPE_ILLEGAL_PWD = "illegal password";
	public static final String ALERT_TYPE_PWD_NOTSAME = "password not the same";
	public static final String ALERT_TYPE_EMPTY = "haven't input";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.signup1);

		// get objects
		mEmail = (EditText) findViewById(R.id.signup_et_email);
		mPassword = (EditText) findViewById(R.id.signup_et_passowrd);
		mConfirmPwd = (EditText) findViewById(R.id.signup_et_confirmPwd);
		mNickName = (EditText) findViewById(R.id.signup_et_nickName);
		mNext = (Button) findViewById(R.id.signup_btn_next1);

		mNext.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Log.v(TAG, "check the user's input");

				// check the user's input before step into step2Activity
				// if there have problems, finish onClick event.
				if (!checkInfo()) {
					return;
				}

				Log.v(TAG, "load the values to user object");
				User user = loadUserProperties();

				Bundle bundle = new Bundle();
				bundle.putSerializable("user", user);

				Intent toStep2Activity = new Intent();
				toStep2Activity.putExtras(bundle);
				toStep2Activity.setClass(SignupStep1Activity.this,
						SignupStep2Activity.class);

				Log.v(TAG, "start activity to signup2");
				startActivity(toStep2Activity);
				Log.v(TAG, "after executed startActivity method.");
			}

		});

	}

	private User loadUserProperties() {
		User user = new User();

		user.setEmail(mEmail.toString());
		user.setPassword(mPassword.toString());
		user.setNickName(mNickName.toString());

		return user;
	}

	/**
	 * check the user's input
	 */
	private boolean checkInfo() {
		String strEmail = mEmail.getText().toString();
		String strPassword = mPassword.getText().toString();
		String strConfirmPwd = mConfirmPwd.getText().toString();
		String strNickName = mNickName.getText().toString();

		Log.v(TAG, "user's input value: strEmail is " + strEmail);
		Log.v(TAG, "user's input value: strPassword is " + strPassword);
		Log.v(TAG, "user's input value: strConfirmPwd is " + strConfirmPwd);
		Log.v(TAG, "user's input value: strNickName is " + strNickName);

		// if any edit text value is empty
		if (strConfirmPwd.equals("") || strEmail.equals("")
				|| strNickName.equals("") || strPassword.equals("")) {
			/*"" == strEmail || "" == strPassword || "" == strConfirmPwd
			|| "" == strNickName*/

			Log.v(TAG, "alert empty");

			AlertDialog alertEmpty = creatBuilder(ALERT_TYPE_EMPTY).create();
			alertEmpty.show();

			return false;
		} else { // else the edit text is not empty

			// if the email isn't illegal
			if (!checkEmail(strEmail)) {
				AlertDialog alertIllegalEmail = creatBuilder(
						ALERT_TYPE_ILLEGAL_EMAIL).create();
				alertIllegalEmail.show();

				return false;
			}

			// if the passwords is illegal
			if (!checkPassword(strPassword) || !checkPassword(strConfirmPwd)) {

				AlertDialog alertIllegalPwd = creatBuilder(
						ALERT_TYPE_ILLEGAL_PWD).create();
				alertIllegalPwd.show();

				return false;

			} else { // else the passwords isn't illegal
				if (!strPassword.equals(strConfirmPwd)) {
					AlertDialog alertPwdNotSame = creatBuilder(
							ALERT_TYPE_PWD_NOTSAME).create();
					alertPwdNotSame.show();

					return false;
				}

			} // // end of else the passwords isn't illegal

			return true;
		} // end of else the edit text is not empty
	}

	/**
	 * Check whether the password is illegal or not
	 * legal password should contain 6 to 15 digits and there must
	 * have a character
	 * @param strPassword	password string
	 * @return return true if legal, or return false.
	 */
	private boolean checkPassword(String strPassword) {
		return strPassword.matches("/^(\\d+\\w+[*/+]*){6,12}$/");
	}

	/**
	 * check the email address format whether is right or not
	 * @param strEmail	email address
	 * @return email address format is right will return true, or return false.
	 */
	private boolean checkEmail(String strEmail) {
		if (strEmail == null || "".equals(strEmail)) {
			return false;
		}

		Log.v(TAG, "value of email:" + strEmail);

		return strEmail
				.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	}

	/**
	 * Builder creator
	 * used for create all kinds of warning dialog in this
	 * activity
	 * 
	 * @param type
	 *            alert type, constants listed on the top area.
	 * @return return a builder
	 */
	private AlertDialog.Builder creatBuilder(String type) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				SignupStep1Activity.this);

		builder.setTitle("警告");

		if (ALERT_TYPE_EMPTY == type) {
			builder.setMessage("请您输入完整的注册信息！");
		} else if (ALERT_TYPE_ILLEGAL_EMAIL == type) {
			builder.setMessage("您输入的邮箱地址不正确，请重新输入！");
		} else if (ALERT_TYPE_ILLEGAL_PWD == type) {
			builder.setMessage("您输入的密码格式不正确，请重新输入！");
		} else if (ALERT_TYPE_PWD_NOTSAME == type) {
			builder.setMessage("您输入的两次密码输入不一致，请重新输入！");
		}
		builder.setPositiveButton("确定", null);

		return builder;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

}
